<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    protected $pathSize=6;

    /**
     * @param $error_code  错误码
     * @param $data         返回的数据
     * @param $msg          提示消息
     * @param $code         http 状态码
     */
    public function writeJson($error_code=0,$data=[],$msg='success',$code=200){

        return response()->json([

            'error_code'=>$error_code,
            'data'=>$data,
            'msg'=>$msg
        ],$code);

    }

    public function sonlevel($data,$pid=0){
        $nodes=[];
        //循环数据找到每一项
        foreach ($data as  $val){
            if ($val['pid']==$pid){
                //如果为顶级 设立下集key 找到下级 以此重复
                $val['son']=$this->sonlevel($data,$val['id']);
                //将当前项  放入新数组
                $nodes[]=$val;
            }
        }

        return $nodes;
    }
}
